package org.jeecg.modules.paycenter.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.paycenter.channel.tonglian.TongLianMemberService;
import org.jeecg.modules.paycenter.channel.tonglian.TongLianPayService;
import org.jeecg.modules.paycenter.channel.tonglian.TongLianUtil;
import org.jeecg.modules.paycenter.enums.UTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.TreeMap;

/**
 * 通联支付服务
 */
@RestController
@Slf4j
@RequestMapping("/paycenter/tl/notify")
public class TongLianNotifyController {

    @Autowired
    private TongLianMemberService memberService;

    @Autowired
    private TongLianPayService payService;

    //货主充值通知
    @RequestMapping("/customerRechargeNotify")
    public String customerRechargeNotify(@RequestBody TreeMap<String, String> map) {
        log.info("货主充值通知：{}", JSON.toJSONString(map));
        try {
            if (TongLianUtil.verifySign(map)) {
                String merOrderNo = map.get("merOrder");
                if (StringUtils.isEmpty(merOrderNo)) {
                    return "success";
                }
                memberService.customerRechargeCallback(merOrderNo, map);
            }
        } catch (Exception e) {
            // 处理异常
            log.error("货主充值通知异常:" + e.getMessage() + "," + JSON.toJSONString(map));
        } finally {
            //收到通知,必须返回success，否则视为未接受通知，会重复通知
            return "success";
        }
    }

    @RequestMapping("/ystPay")
    public String ystPay(@RequestBody TreeMap<String, String> map) {
//        log.info("付款回调通知：{}", JSON.toJSONString(map));
        try {
            if (TongLianUtil.verifySign(map)) {
                String merOrderNo = map.get("merOrder");
                if (StringUtils.isEmpty(merOrderNo)) {
                    return "success";
                }
                String uType = merOrderNo.charAt(0) + "";
                UTypeEnum uTypeEnum = UTypeEnum.getUTypeEnum(uType);
                switch (uTypeEnum) {
                    case DRIVER:
                        payService.payShippingNoteCallback(merOrderNo , map);
                        break;
                    case CARRIER:
                        //todo tl
                        throw new JeecgBootException("todo");
                    default:
                        log.error("付款回调通知异常:未知用户类型," + JSON.toJSONString(map));
                        break;
                }
            }
        } catch (Exception e) {
            // 处理异常
            log.error("付款回调通知异常:" + e.getMessage() + "," + JSON.toJSONString(map));
        } finally {
            //收到通知,必须返回success，否则视为未接受通知，会重复通知
            return "success";
        }
    }
}
